--[[ - Remade by Tronex - 2019/4/23 - Global functions for smart control tasks, with support for pre-info before accepting - Smart control tasks are similar to Assault, but they target specific smart/s rather than scanning the alife Parameters for precondition P[1] = (string) task_id P[2] = (num) minimum number of squads on a smart P[3] = (num) minumum stay time for squad P[4] = (bool) if true, scan will includes scripted squads P[5] = (bool) if true, the factions declared in "status_functor_params" are enemy factions to traget, otherwise script assume they are natural and will search for matual enemies to them. If first faction is "monster" it will be a mutant squad hunt Example of usage in configs: precondition = validate_smart_control_task( mil_smart_terrain_7_7_freedom_leader_stalker_task_2 : 2 : nil : true : true ) target_functor = smart_control_task_target_functor status_functor = smart_control_task_status_functor status_functor_params = dolg status_functor_smarts = red_smart_terrain_6_3 on_job_descr = %=setup_smart_control_task( mil_smart_terrain_7_7_freedom_leader_stalker_task_2 )% --]] -- Cache on setup local cache = {} local cache_enemy = {} local cache_scripted = {} -- Cache during task local cache_info = {} local cache_func = {} local cache_smarts = {} local sfind = string.find local factions_list = { -- List of allowed factions ["stalker"] = true, ["dolg"] = true, ["freedom"] = true, ["csky"] = true, ["ecolog"] = true, ["killer"] = true, ["army"] = true, ["bandit"] = true, ["monolith"] = true, } ---------------------------< Utility >--------------------------- function is_legit_mutant_squad(squad) local section = squad and squad:section_name() return squad and (not sfind(section,"tushkano")) and (not sfind(section,"rat")) and true or false end function evaluate_smarts_squads(task_id, tbl, smart, squad_def, faction_def) if (not smart) then return end local smrt_id = smart.id local smrt_name = smart:name() local smrt = smrt_id and SIMBOARD.smarts[smrt_id] if (not smrt) then return end --printf("~ %s | scanning smart: %s", task_id, smrt_name) for sq_id,_ in pairs(smrt.squads) do --printf("# %s | found squad (%s) in smart: %s", task_id, sq_id, smrt_name) -- if smart's squad is on its level + they are targeting it local squad = alife_object(sq_id) if squad and simulation_objects.is_on_the_same_level(squad, smart) and squad.current_target_id and (squad.current_target_id == smrt_id) and (squad.current_action == 1) and squad.stay_time and ((not squad_def.stay_time) or (squad_def.stay_time and (game.get_game_time():diffSec(squad.stay_time) <= tonumber(squad_def.stay_time)))) and (squad_def.scripted or (not squad:get_script_target())) then --printf("# %s | smart (%s) [%s] w/ squad (%s) [%s] = Checking", task_id, smrt_id, smrt_name, sq_id, squad.player_id) for fac,_ in pairs(faction_def) do -- if squad is from enemies table if (is_legit_mutant_squad(squad) and squad.player_id == fac) then --squad.stay_time = game.get_game_time() tbl[#tbl + 1] = sq_id --printf("- %s | smart (%s) [%s] w/ squad (%s) [%s] = Added", task_id, smrt_id, smrt_name, sq_id, squad.player_id) end end end end end function evaluate_squads_smarts(task_id, var, smart, smrt) for sq_id,_ in pairs(smrt.squads) do local squad = alife_object(sq_id) if squad and simulation_objects.is_on_the_same_level(squad, smart) then if not ( squad.first_update ) then --printf("~ %s | not all squads are loaded yet!", task_id) return true end if (var.scripted or (not squad:get_script_target())) and (squad.current_target_id and squad.current_target_id == smart.id and squad.current_action == 1) then --printf("- %s | squad (%s) [%s] is targeting smart (%s)", task_id, squad.id, squad.player_id, smart.id) for i = 1, #cache_func[task_id] do local fac = cache_func[task_id][i] if (is_legit_mutant_squad(squad) and squad.player_id == fac) then -- reset gametime so they don't leave squad.stay_time = game.get_game_time() squad.force_online = true --printf("- %s | squad (%s) [%s] is saved", task_id, squad.id, squad.player_id) return sq_id end end end end end return false end function postpone_for_next_frame(task_id, squad_id) local squad = alife_object(squad_id) if (squad) then -- Location local location = alife():level_name(game_graph():vertex(squad.m_game_vertex_id):level_id()) for k in squad:squad_members() do local se_obj = k.object or alife_object(k.id) if se_obj then location = dynamic_news_helper.GetPointDescription(se_obj) break end end local str_location = game.translate_string("st_location") .. " " .. location -- Community local str_comm = "" local community = squad.player_id if is_squad_monster[community] then str_comm = game.translate_string("st_sq_type") .. " " .. game.translate_string(community) else str_comm = game.translate_string("st_mm_new_game_faction_2") .. " " .. game.translate_string(community) end -- Build News local news_caption = game.translate_string(task_manager.task_ini:r_string_ex(task_id, "title")) or "error" local news_text = str_comm .. "\\n " .. str_location local news_ico = news_manager.tips_icons[squad.player_id] or task_manager.task_ini:r_string_ex(task_id, "icon") or "ui_iconsTotal_mutant" --local news_ico = task_manager.task_ini:r_string_ex(task_id, "icon") or "ui_iconsTotal_mutant" --local news_text = str_location db.actor:give_talk_message2(news_caption, news_text, news_ico, "iconed_answer_item") end return true end ---------------------------< Target functor >--------------------------- task_functor.smart_control_task_target_functor = function (task_id,field,p,tsk) if (field == "target") then if (tsk and tsk.stage == 1 and tsk.task_giver_id) then return tsk.task_giver_id end local actor = db.actor local var = actor and load_var(actor, task_id) if (not var) then printe("! %s | assault_task_target_functor - var is nil", task_id) return end local smart_id = cache_info.smart_id local smart = smart_id and alife_object(smart_id) if (not smart) then printe("! %s | assault_task_target_functor - smart is nil", task_id) return end local squad_id = cache_info.squad_id local squad = squad_id and alife_object(squad_id) if (not squad) then printe("! %s | assault_task_target_functor - squad is nil", task_id) return end -- If enemies are more then 50m from target location, then show red (?) near their location if squad then if (smart.position:distance_to_sqr(squad.position) > 2500) then if (squad:clsid() == clsid.online_offline_group_s) then if (squad.id and level.map_has_object_spot(squad.id,"red_location") == 0) then level.map_add_object_spot(squad.id, "red_location", "st_ui_pda_task_unknown_enemy") end end else if (squad:clsid() == clsid.online_offline_group_s) then if (squad.id and level.map_has_object_spot(squad.id,"red_location") == 1) then level.map_remove_object_spot(squad.id, "red_location") end end end end return smart_id end end ---------------------------< Status functor >--------------------------- task_status_functor.smart_control_task_status_functor = function (tsk,task_id) if not (db.actor and tsk) then return end if (tsk.stage == 1) then return end -- already completed local var = load_var(db.actor, task_id) -- check saved data if (not var) then return "fail" end -- Store factions parameters for the first time to re-use it if (not cache_func[task_id]) then cache_func[task_id] = {} local params = parse_list(task_manager.task_ini,task_id,"status_functor_params") if var.is_enemy then for i=1,#params do if is_squad_monster[params[i]] or factions_list[params[i]] then cache_func[task_id][i] = params[i] printf("/ %s | Faction [%s] is re-added to cache_func table", task_id, params[i]) end end elseif (not is_squad_monster[params[1]]) then for fac,_ in pairs(factions_list) do local cnt = 0 local is_enemy_to_actor = true --game_relations.is_factions_enemies(fac, get_actor_true_community()) for i=1,#params do if (fac ~= params[i]) and is_enemy_to_actor and game_relations.is_factions_enemies(fac, params[i]) then cnt = cnt + 1 end end if (cnt == #params) then local idx = #cache_func[task_id] + 1 cache_func[task_id][idx] = fac printf("/ %s | Faction [%s] is re-added to cache_func table", task_id, fac) end end end if (#cache_func[task_id] == 0) then printf("! %s | no enemy factions found",task_id) return "fail" end end if (not cache_smarts[task_id]) then cache_smarts[task_id] = parse_list(task_manager.task_ini,task_id,"status_functor_smarts") end -- Timer for less pressure local tg = time_global() if (tsk.__check_smart_time and tg < tsk.__check_smart_time) then return end tsk.__check_smart_time = tg + 3000 -- Scan all target smarts for z=1, #cache_smarts[task_id] do local smart_name = cache_smarts[task_id][z] -- check smart id local smrt = SIMBOARD.smarts[smart_name] -- check smart object (special defines) if (not smrt) then return "fail" end local smart = smrt.smrt -- check smart server object if (not smart) then return "fail" end -- in case sim_avail is set true during the player's tsk. with simulation_objects.available_by_id[smart.id] nil means unprocess, false is absolutely not avail if (simulation_objects.available_by_id[smart.id] == nil) then return elseif (simulation_objects.available_by_id[smart.id] == false) then printe("! %s | task failed because smart no longer available", task_id) return "fail" end -- cleaning data for re-assign next local squad_id = evaluate_squads_smarts(task_id, var, smart, smrt) if squad_id then cache_info[task_id]= { smart_id = smart.id , squad_id = squad_id } return end -- If smart is controllable by factions, player dominate it if (smart.faction_controlled) then local comm = character_community(db.actor):sub(7) smart.faction = factions_list[comm] and comm or smart.faction end end -- Assuming no squads means that target smarts got cleared tsk.stage = 1 end ---------------------------< Precondition >--------------------------- xr_conditions.validate_smart_control_task = function(actor, npc, p) if not (p and #p >= 1) then return false end local task_id = p[1] if (#p < 5) then printe("! %s | not enough parameters", task_id) return false end --// Check cache if cache[task_id] then return true end --// Utilities local is_avail = simulation_objects.available_by_id local p_status = parse_list(task_manager.task_ini,task_id,"status_functor_params") local enemy_faction_list = {} if (not p_status[1]) then printe("! %s | status functor parameters are mising!", task_id) return false end --// Defines local def = {} def.num = tonumber(p[2]) or 1 def.stay_time = (p[3] ~= "nil") and tonumber(p[3]) def.scripted = (p[4] == "true") and true or false def.is_enemy = (p[5] == "true") and true or false def.smart = parse_list(task_manager.task_ini,task_id,"status_functor_smarts") cache_enemy[task_id] = def.is_enemy cache_scripted[task_id] = def.scripted --// Collect enemy factions if def.is_enemy then -- if faction parameters are enemies for i=1,#p_status do if is_squad_monster[p_status[i]] or factions_list[p_status[i]] then --printf("/ %s | Faction [%s] is added to enemy_faction_list table", task_id, p_status[i]) enemy_faction_list[p_status[i]] = true end end elseif (not is_squad_monster[p_status[1]]) then -- if faction parameters are matutal factions for fac,_ in pairs(factions_list) do local cnt = 0 local is_enemy_to_actor = true --game_relations.is_factions_enemies(fac, get_actor_true_community()) for i=1,#p_status do if (fac ~= p_status[i]) and is_enemy_to_actor and game_relations.is_factions_enemies(fac, p_status[i]) then cnt = cnt + 1 end end if (cnt == #p_status) then enemy_faction_list[fac] = true --printf("/ %s | Faction [%s] is added to enemy_faction_list table", task_id, fac) end end end if is_empty(enemy_faction_list) then printf("! %s | no enemy factions found", task_id) return false end --// Search all smarts for smart_name, _ in pairs(def.smart) do local targets = {} -- target[squad_id] = smart_name local smart = SIMBOARD.smarts_by_names[smart_name] if smart and (is_avail[smart.id] == true) then evaluate_smarts_squads(task_id, targets, smart, def, enemy_faction_list) end --// Cache results if (#targets >= def.num) then if (not cache[task_id]) then cache[task_id] = {} end cache[task_id][smart_name] = {} copy_table(cache[task_id][smart_name], targets) --printf("- %s | Smart [%s] has %s squads", task_id, smart_name, size_table(targets)) end end if cache[task_id] then return true end --printf("! %s | no targets found", task_id) return false end ---------------------------< Effects >--------------------------- xr_effects.setup_smart_control_task = function(actor, npc, p) local task_id = p[1] if not (task_id and cache[task_id]) then return false end --// Read cache local smarts = {} local squad_id for smart_name, targets in pairs(cache[task_id]) do smarts[#smarts + 1] = smart_name if (not squad_id) then squad_id = targets[math.random(#targets)] end for i=1,#targets do local id = targets[i] local sq = alife_object(id) if sq then sq.stay_time = game.get_game_time() sim_offline_combat.task_squads[id] = true end end end --// Save var local squad = alife_object(squad_id) if squad then local is_enemy = cache_enemy[task_id] and true or false local scripted = cache_scripted[task_id] and true or false local tbl = { is_enemy = is_enemy, scripted = scripted, } save_var(db.actor, task_id, tbl) printdbg("- %s | Cached result = squad_id (%s) [%s] - smart_id(%s) - is_enemy: %s - scripted: %s", task_id, squad_id, squad.player_id, smart_id, is_enemy, scripted) CreateTimeEvent(0,"setup_assault_task",0,postpone_for_next_frame,task_id, squad_id) end end